package com.linewell.controller;

import com.linewell.entity.Article;
import com.linewell.entity.Tag;
import com.linewell.service.IArticleService;
import com.linewell.service.ITagService;
import com.linewell.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Title: ssm2
 * @description: 标签控制器
 * @author: hyuhuang@linewell.com
 * @since:2018/2/5
 */
@Controller
public class TagController {

    @Autowired
    ITagService tagService;
    @Autowired
    IArticleService articleService;
    /**
     * @descripition  获得所有热门的标签
     * @author hyuhuang@linewell.com
     * @date 2018/2/27 10:54
     * @return com.linewell.utils.Msg
     */
    @RequestMapping(value = "/getHotTags",method = RequestMethod.GET)
    @ResponseBody
    public Msg getHotTag(){
        List<Tag> tagsList = tagService.getHotTag();
        return Msg.success().add("tagsList",tagsList);
    }
    /**
     * @descripition  获得热门标签
     * @author hyuhuang@linewell.com
     * @date 2018/2/27 10:54
     * @return com.linewell.utils.Msg
     */
    @RequestMapping("/getCountTag")
    @ResponseBody
    public Msg getCountTag(){
        List<Tag> tagList = tagService.getHotTag();
        return Msg.success().add("text",tagList);
    }
    /**
     * @descripition  根据标签id来筛选标签内容
     * @author hyuhuang@linewell.com
     * @date 2018/3/25 11:53
     * @param request
     * @return com.linewell.utils.Msg
     */
    @RequestMapping(value = "/getArticleByTagsId",method = RequestMethod.GET)
    @ResponseBody
    public Msg selectArticleTag(HttpServletRequest request){
        Long tagId = Long.valueOf(request.getParameter("tagId"));
        int page = Integer.parseInt(request.getParameter("page"));
        List<Article> articleList = articleService.selectByTagId(tagId,page);
        return Msg.success().add("articleList",articleList);
    }
    /**
     * @descripition  获取标签
     * @author hyuhuang@linewell.com
     * @date 2018/3/25 11:54
     * @param
     * @return com.linewell.utils.Msg
     */
    @RequestMapping(value = "/getTags",method = RequestMethod.GET)
    @ResponseBody
    public Msg getTags(){
        return tagService.getTags();
    }
    /**
     * @descripition  插入multilabel表
     * @author hyuhuang@linewell.com
     * @date 2018/3/25 11:54
     * @param tagId, articleId
     * @return void
     */
    @RequestMapping(value = "/addMulti",method = RequestMethod.GET)
    @ResponseBody
    public void addMulti(@RequestParam("tagId")int tagId,@RequestParam("articleId")long articleId,@RequestParam("userId")long userId){
        tagService.insertMutil(userId,articleId,tagId);
    }
    /**
     * @descripition  判断是否存在标签，不存在存入，存在则存入multilabel表
     * @author hyuhuang@linewell.com
     * @date 2018/3/25 11:53
     * @param tagName, articleId
     * @return void
     */
    @RequestMapping(value = "/addTagAndMulti",method = RequestMethod.GET)
    @ResponseBody
    public void addTagAndMulti(@RequestParam("tagName") String tagName, @RequestParam("articleId")long articleId){
        List<Tag> tag1 = tagService.selectTagByTagName(tagName);
        if (tag1.size() == 0 ){
            tagService.insertTag(tagName);
        }
        List<Tag> tag2 = tagService.selectTagByTagName(tagName);
        tagService.insertMutilByArticle(articleId,tag2.get(0).getTagId());
    }
}
